package org.jeecg.modules.pos.controller;

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.hutool.core.io.FastByteArrayOutputStream;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.weaver.ast.Var;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.*;
import org.jeecg.modules.business.entity.BusHotel;
import org.jeecg.modules.business.service.IBusHotelService;
import org.jeecg.modules.mall.entity.MallUserInfo;
import org.jeecg.modules.mall.entity.WxAppConfig;
import org.jeecg.modules.mall.ma.WxMaInRedisConfigStorage;
import org.jeecg.modules.mall.service.IWxAppConfigService;
import org.jeecg.modules.pos.PosBookingStateEnum;
import org.jeecg.modules.pos.entity.*;
import org.jeecg.modules.pos.service.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: pos_table
 * @Author: jeecg-boot
 * @Date:   2023-04-11
 * @Version: V1.0
 */
@Api(tags="pos_table")
@RestController
@RequestMapping("/pos/posTable")
@Slf4j
public class PosTableController extends JeecgController<PosTable, IPosTableService> {
	 @Autowired
	 private IPosTableService posTableService;
	 @Autowired
	 private IPosTypeService posTypeService;
	 @Autowired
	 private IPosRegionService posRegionService;
	 @Autowired
	 private IPosTableTypeService posTableTypeService;
	 @Autowired
	 private IPosOrderGoodsService posOrderGoodsService;
	 @Resource
	 IPosBookingService bookingService;
	 @Resource
	 private IWxAppConfigService wxAppConfigService;
	 @Resource
	 private RedisTemplate redisTemplate;

	 /**
	  * 分页列表查询
	  *
	  * @param posTable
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 //@AutoLog(value = "pos_table-分页列表查询")
	 @ApiOperation(value = "pos_table-分页列表查询", notes = "pos_table-分页列表查询")
	 @GetMapping(value = "/list")
	 public Result<IPage<PosTable>> queryPageList(PosTable posTable,
												  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
												  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
												  HttpServletRequest req) {
		 QueryWrapper<PosTable> queryWrapper = QueryGenerator.initQueryWrapper(posTable, req.getParameterMap());
		 Page<PosTable> page = new Page<PosTable>(pageNo, pageSize);
		 IPage<PosTable> pageList = posTableService.page(page, queryWrapper);
		 pageList.getRecords().forEach(item -> {
			 PosType type = posTypeService.getById(item.getPosTypeId());
			 if (type != null) {
				 item.setTypeName(type.getName());
			 }
			 PosRegion region = posRegionService.getById(item.getPosRegionId());
			 if (region != null) {
				 item.setRegionName(region.getName());
			 }
			 PosTableType tableType = posTableTypeService.getById(item.getPosTableTypeId());
			 if (tableType != null) {
				 item.setTableTypeName(tableType.getName());
			 }
		 });
		 return Result.OK(pageList);
	 }

	 /**
	  * 查询桌台用餐列表
	  * @param posTable
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @ApiOperation(value = "pos_table-分页列表查询", notes = "pos_table-分页列表查询")
	 @GetMapping(value = "/tableList")
	 public Result<IPage<PosTable>> tableList(PosTable posTable,
											  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
											  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
											  HttpServletRequest req) {
		 QueryWrapper<PosTable> queryWrapper = QueryGenerator.initQueryWrapper(posTable, req.getParameterMap());
		 Page<PosTable> page = new Page<PosTable>(pageNo, pageSize);
		 IPage<PosTable> pageList = posTableService.page(page, queryWrapper);
		 // 找出今日所有预约的桌台id集合
		 List<String> bookingTableIds = getBookingTableIds();
		 pageList.getRecords().forEach(item -> {
			 // 是否是预约单
			 item.setIsBooking(bookingTableIds.contains(item.getId()));
			 // 如果桌台状态是 待下单、待结算、已经预结 就查询订单
			 if (!item.getState().equals(0) && !item.getState().equals(4)) {
				 PosOrderGoods orderGoods = posOrderGoodsService.getOrderTeamByTableId(item.getId());
				 item.setPosOrderGoods(orderGoods);
			 }
		 });
		 return Result.OK(pageList);
	 }


	 /**
	  * 查询可以联台的桌台列表
	  * @param posTable
	  * @param pageNo
	  * @param pageSize
	  * @return
	  */
	 @ApiOperation(value = "pos_table-分页列表查询", notes = "pos_table-分页列表查询")
	 @GetMapping(value = "/getCanUseTable")
	 public Result<IPage<PosTable>> getCanUseTable(PosTable posTable,
											  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
											  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
		 if (StrUtil.isEmpty(posTable.getId())){
			 throw new JeecgBootException("请选择要操作的桌台");
		 }
		 LambdaQueryWrapper<PosTable> wrapper = new LambdaQueryWrapper<>();
		 wrapper.eq(PosTable::getPosTypeId, posTable.getPosTypeId());
		 wrapper.eq(StrUtil.isNotEmpty(posTable.getPosRegionId()),PosTable::getPosRegionId, posTable.getPosRegionId());
		 wrapper.eq(PosTable::getHotelId, posTable.getHotelId());
		 wrapper.ne(PosTable::getId, posTable.getId());
		 // 换台可选：0空桌。 转菜可选：2待结账的。 联台可选：1待下单、2待结账、3已预结。
		 if (posTable.getState() == 5){
			 // 查询可联台的桌台，前端传入state = 5
			 wrapper.and(i -> i.eq(PosTable::getState,1).or().eq(PosTable::getState,2).or().eq(PosTable::getState,3));
		 } else {
			 wrapper.eq(ObjectUtil.isNotEmpty(posTable.getState()),PosTable::getState,posTable.getState());
		 }
		 Page<PosTable> page = new Page<PosTable>(pageNo, pageSize);
		 IPage<PosTable> pageList = posTableService.page(page, wrapper);
		 // 找出今日所有预约的桌台id集合
		 List<String> bookingTableIds = getBookingTableIds();
		 pageList.getRecords().forEach(item -> {
			 // 是否是今日有预约单
			 item.setIsBooking(bookingTableIds.contains(item.getId()));
			 PosOrderGoods orderGoods = posOrderGoodsService.getOrderTeamByTableId(item.getId());
			 item.setPosOrderGoods(orderGoods);
		 });
		 return Result.OK(pageList);
	 }

	 /**
	  * 添加
	  *
	  * @param posTable
	  * @return
	  */
	 @AutoLog(value = "pos_table-添加")
	 @ApiOperation(value = "pos_table-添加", notes = "pos_table-添加")
	 //@RequiresPermissions("pos:pos_table:add")
	 @PostMapping(value = "/add")
	 public Result<String> add(@RequestBody PosTable posTable) {
		 if (posTable.getTenantId() == null || posTable.getTenantId().equals("")) {
			 LoginUser user = TokenUtils.getAuthUser();
			 if (user.getRelTenantIds() != null && !user.getRelTenantIds().equals("")) {
				 posTable.setTenantId(user.getRelTenantIds());
			 } else {
				 throw new JeecgBootException("当前登录人租户信息错误");
			 }
		 }

		 posTable.setId(UUIDGenerator.generate());
//		 LambdaQueryWrapper<WxAppConfig> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//		 lambdaQueryWrapper.eq(WxAppConfig::getTenantId, posTable.getTenantId());
//		 WxAppConfig wxApp = wxAppConfigService.getOne(lambdaQueryWrapper);
//		 if (wxApp == null) {
//			 throw new JeecgBootException("没有找到此appid");
//		 }
//		 //获取小程序二维码
//		 try {
//			 WxMaInRedisConfigStorage configStorage = new WxMaInRedisConfigStorage(redisTemplate);
//			 configStorage.setAppid(wxApp.getAppId());
//			 configStorage.setSecret(wxApp.getAppSecret());
//			 WxMaService wxMaService = new WxMaServiceImpl();
//			 wxMaService.setWxMaConfig(configStorage);
//
//			 File file = wxMaService.getQrcodeService().createQrcode("page/dc/index?id=" + posTable.getId(), 430);
//			 try {
//				 MultipartFile cMultiFile = new MockMultipartFile("file", file.getName(), null, new FileInputStream(file));
//				 String path = CommonUtils.upload(cMultiFile, "temp", CommonConstant.UPLOAD_TYPE_OSS);
//				 posTable.setQrCode(path);
//			 } catch (IOException e) {
//				 e.printStackTrace();
//			 }
//
//		 } catch (WxErrorException e) {
//			 e.printStackTrace();
//
//			 throw new JeecgBootException("获取小程序二维码失败：" + e.getMessage());
//		 }
		 posTableService.save(posTable);
		 return Result.OK("添加成功！");
	 }

	 @AutoLog(value = "pos_table-添加")
	 @ApiOperation(value = "pos_table-添加", notes = "pos_table-添加")
	 //@RequiresPermissions("pos:pos_table:add")
	 @PostMapping(value = "/addBatch")
	 public Result<String> addBatch(@RequestBody List<PosTable> posTables) {
		 if (posTables.size() > 100) {
			 return Result.error("批量添加不能超过100个桌台！");
		 }
		 posTables.forEach(posTable -> {
			 if (posTable.getTenantId() == null || posTable.getTenantId().equals("")) {
				 LoginUser user = TokenUtils.getAuthUser();
				 if (user.getRelTenantIds() != null && !user.getRelTenantIds().equals("")) {
					 posTable.setTenantId(user.getRelTenantIds());
				 } else {
					 throw new JeecgBootException("当前登录人租户信息错误");
				 }
			 }

//			 posTable.setId(UUIDGenerator.generate());
//			 LambdaQueryWrapper<WxAppConfig> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//			 lambdaQueryWrapper.eq(WxAppConfig::getTenantId, posTable.getTenantId());
//			 WxAppConfig wxApp = wxAppConfigService.getOne(lambdaQueryWrapper);
//			 if (wxApp == null) {
//				 throw new JeecgBootException("没有找到此appid");
//			 }
//			 //获取小程序二维码
//			 try {
//				 WxMaInRedisConfigStorage configStorage = new WxMaInRedisConfigStorage(redisTemplate);
//				 configStorage.setAppid(wxApp.getAppId());
//				 configStorage.setSecret(wxApp.getAppSecret());
//				 WxMaService wxMaService = new WxMaServiceImpl();
//				 wxMaService.setWxMaConfig(configStorage);
//
//				 File file = wxMaService.getQrcodeService().createQrcode("page/dc/index?id=" + posTable.getId(), 430);
//				 try {
//					 MultipartFile cMultiFile = new MockMultipartFile("file", file.getName(), null, new FileInputStream(file));
//					 String path = CommonUtils.upload(cMultiFile, "temp", CommonConstant.UPLOAD_TYPE_OSS);
//					 posTable.setQrCode(path);
//				 } catch (IOException e) {
//					 e.printStackTrace();
//				 }
//
//			 } catch (WxErrorException e) {
//				 e.printStackTrace();
//
//				 throw new JeecgBootException("获取小程序二维码失败：" + e.getMessage());
//			 }
		 });
		 posTableService.saveBatch(posTables);
		 return Result.OK("添加成功！");
	 }


	 /**
	  * 桌台用餐下单
	  *
	  * @param posTable
	  * @return
	  */
	 @AutoLog(value = "pos_table-桌台用餐下单")
	 @ApiOperation(value = "pos_table-桌台用餐下单", notes = "pos_table-桌台用餐下单")
	 @RequestMapping(value = "/addOrder", method = {RequestMethod.PUT, RequestMethod.POST})
	 public Result<String> addOrder(@RequestBody PosTable posTable) {
		 PosTable table = posTableService.getById(posTable.getId());
		 if (!table.getState().equals(0)) {
			 return Result.error("不是空桌状态，不能下单!");
		 }
		 PosOrderGoods orderGoods = new PosOrderGoods();
		 orderGoods.setCode(randomNumber("P"));
		 orderGoods.setPosTableId(posTable.getId());
		 posTable.setState(1);
		 posTable.setOrderTime(new Date());
		 posOrderGoodsService.save(orderGoods);
		 posTableService.updateById(posTable);
		 return Result.OK("下单成功!");
	 }

	 /**
	  * 生成16位数字+prefix
	  * @param prefix
	  * @return
	  */
	 private String randomNumber(String prefix) {
		 int first = new Random(10).nextInt(8) + 1;
		 int hashCode = UUID.randomUUID().toString().hashCode();
		 if (hashCode < 0) {
			 hashCode = -hashCode;
		 }
		 return prefix + first + String.format("%015d", hashCode);
	 }

	 /**
	  * 编辑
	  *
	  * @param posTable
	  * @return
	  */
	 @AutoLog(value = "pos_table-编辑")
	 @ApiOperation(value = "pos_table-编辑", notes = "pos_table-编辑")
	 //@RequiresPermissions("pos:pos_table:edit")
	 @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
	 public Result<String> edit(@RequestBody PosTable posTable) {
		 posTableService.updateById(posTable);
		 return Result.OK("编辑成功!");
	 }

	 /**
	  * 通过id删除
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "pos_table-通过id删除")
	 @ApiOperation(value = "pos_table-通过id删除", notes = "pos_table-通过id删除")
	 //@RequiresPermissions("pos:pos_table:delete")
	 @DeleteMapping(value = "/delete")
	 public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
		 posTableService.removeById(id);
		 return Result.OK("删除成功!");
	 }

	 /**
	  * 批量删除
	  *
	  * @param ids
	  * @return
	  */
	 @AutoLog(value = "pos_table-批量删除")
	 @ApiOperation(value = "pos_table-批量删除", notes = "pos_table-批量删除")
	 //@RequiresPermissions("pos:pos_table:deleteBatch")
	 @DeleteMapping(value = "/deleteBatch")
	 public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
		 this.posTableService.removeByIds(Arrays.asList(ids.split(",")));
		 return Result.OK("批量删除成功!");
	 }

	 /**
	  * 通过id查询
	  *
	  * @param id
	  * @return
	  */
	 //@AutoLog(value = "pos_table-通过id查询")
	 @ApiOperation(value = "pos_table-通过id查询", notes = "pos_table-通过id查询")
	 @GetMapping(value = "/queryById")
	 public Result<PosTable> queryById(@RequestParam(name = "id", required = true) String id) {
		 PosTable posTable = posTableService.getById(id);
		 if (posTable == null) {
			 return Result.error("未找到对应数据");
		 }
		 if (posTable.getState().equals(2)) {
			 LambdaQueryWrapper<PosOrderGoods> lambdaQueryWrapper = new LambdaQueryWrapper<>();
			 lambdaQueryWrapper.eq(PosOrderGoods::getPosTableId, posTable.getId());
			 lambdaQueryWrapper.orderByDesc(PosOrderGoods::getCreateTime);
			 Page<PosOrderGoods> page2 = new Page<PosOrderGoods>(1, 1);
			 lambdaQueryWrapper.orderByDesc(PosOrderGoods::getCreateTime);
			 IPage<PosOrderGoods> posOrderGoodsList = posOrderGoodsService.page(page2, lambdaQueryWrapper);
			 if (ObjectUtils.isNotEmpty(posOrderGoodsList.getRecords())) {
				 posTable.setPosOrderGoods(posOrderGoodsList.getRecords().get(0));
			 }
		 }
		 return Result.OK(posTable);
	 }


	 /**
	  * 获取今日预定单预定桌id
	  */
	 private List<String> getBookingTableIds() {
		 List<PosBookingOrder> bookingOrders = bookingService.getTodayBooking();
		 List<String> codes = bookingOrders.stream().map(PosBookingOrder::getCode).collect(Collectors.toList());
		 if (codes.isEmpty()){
			 return new ArrayList<>();
		 }
		 List<PosOrderGoods> orderGoods = posOrderGoodsService.list(Wrappers.<PosOrderGoods>lambdaQuery().in(PosOrderGoods::getBookingCode, codes));
		 return orderGoods.stream().map(PosOrderGoods::getPosTableId).collect(Collectors.toList());
	 }


	 /**
	  * 生成二维码
	  *
	  * @param id
	  * @return
	  */
	 //@AutoLog(value = "pos_table-通过id查询")
	 @ApiOperation(value = "生成二维码", notes = "生成二维码")
	 @GetMapping(value = "/generateQRCode")
	 public Result<PosTable> generateQRCode (@RequestParam(name = "id", required = true) String id) {
		 PosTable posTable = posTableService.getById(id);
		 if (posTable == null) {
			 return Result.error("未找到对应数据");
		 }
		 if (StringUtils.isBlank(posTable.getQrCode())) {
			 LambdaQueryWrapper<WxAppConfig> lambdaQueryWrapper = new LambdaQueryWrapper<>();
			 lambdaQueryWrapper.eq(WxAppConfig::getTenantId, posTable.getTenantId());
			 WxAppConfig wxApp = wxAppConfigService.getOne(lambdaQueryWrapper);
			 if (wxApp == null) {
				 throw new JeecgBootException("没有找到此appid");
			 }
			 //获取小程序二维码
			 try {
				 WxMaInRedisConfigStorage configStorage = new WxMaInRedisConfigStorage(redisTemplate);
				 configStorage.setAppid(wxApp.getAppId());
				 configStorage.setSecret(wxApp.getAppSecret());
				 WxMaService wxMaService = new WxMaServiceImpl();
				 wxMaService.setWxMaConfig(configStorage);

				 File file = wxMaService.getQrcodeService().createQrcode("page/dc/index?id=" + posTable.getId(), 430);
				 try {
					 MultipartFile cMultiFile = new MockMultipartFile("file", file.getName(), null, new FileInputStream(file));
					 String path = CommonUtils.upload(cMultiFile, "temp", CommonConstant.UPLOAD_TYPE_OSS);
					 posTable.setQrCode(path);
					 posTableService.updateById(posTable);
				 } catch (IOException e) {
					 e.printStackTrace();
				 }

			 } catch (WxErrorException e) {
				 e.printStackTrace();

				 throw new JeecgBootException("获取小程序二维码失败：" + e.getMessage());
			 }
		 }
		 return Result.OK(posTable);
	 }

	 /**
	  * 导出excel
	  *
	  * @param request
	  * @param posTable
	  */
	 //@RequiresPermissions("pos:pos_table:exportXls")
	 @RequestMapping(value = "/exportXls")
	 public ModelAndView exportXls(HttpServletRequest request, PosTable posTable) {
		 return super.exportXls(request, posTable, PosTable.class, "pos_table");
	 }

	 /**
	  * 通过excel导入数据
	  *
	  * @param request
	  * @param response
	  * @return
	  */
	 //@RequiresPermissions("pos:pos_table:importExcel")
	 @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	 public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		 return super.importExcel(request, response, PosTable.class);
	 }

	 @ApiOperation(value = "批量下载二维码压缩包", notes = "批量下载二维码压缩包")
	 @GetMapping(value = "/makeQRCode")
	 public void makeQRCode(HttpServletResponse response, @RequestParam(name = "ids", required = true) String ids) {
		 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		 ZipOutputStream zip = new ZipOutputStream(outputStream);
		 List<PosTable> posTables = this.posTableService.listByIds(Arrays.asList(ids.split(",")));
		 for (PosTable posTable : posTables) {
			 if (StringUtils.isBlank(posTable.getQrCode())) {
				 LambdaQueryWrapper<WxAppConfig> lambdaQueryWrapper = new LambdaQueryWrapper<>();
				 lambdaQueryWrapper.eq(WxAppConfig::getTenantId, posTable.getTenantId());
				 WxAppConfig wxApp = wxAppConfigService.getOne(lambdaQueryWrapper);
				 if (wxApp == null) {
					 throw new JeecgBootException("没有找到此appid");
				 }
				 //获取小程序二维码
				 try {
					 WxMaInRedisConfigStorage configStorage = new WxMaInRedisConfigStorage(redisTemplate);
					 configStorage.setAppid(wxApp.getAppId());
					 configStorage.setSecret(wxApp.getAppSecret());
					 WxMaService wxMaService = new WxMaServiceImpl();
					 wxMaService.setWxMaConfig(configStorage);

					 File file = wxMaService.getQrcodeService().createQrcode("page/dc/index?id=" + posTable.getId(), 430);
					 try {
						 MultipartFile cMultiFile = new MockMultipartFile("file", file.getName(), null, new FileInputStream(file));
						 String path = CommonUtils.upload(cMultiFile, "temp", CommonConstant.UPLOAD_TYPE_OSS);
						 posTable.setQrCode(path);
						 posTableService.updateById(posTable);
					 } catch (IOException e) {
						 e.printStackTrace();
					 }

				 } catch (WxErrorException e) {
					 e.printStackTrace();

					 throw new JeecgBootException("获取小程序二维码失败：" + e.getMessage());
				 }
			 }

			 if (StringUtils.isNotBlank(posTable.getQrCode())) {
				 URL url3 = null;
				 try {
					 url3 = new URL(posTable.getQrCode());

					 //得到图片的流
					 InputStream stream = URLUtil.getStream(url3);
					 FastByteArrayOutputStream read = IoUtil.read(stream);
					 byte[] bytes = read.toByteArray();
					 try {
						 // 文件流添加到zip流
						 zip.putNextEntry(new ZipEntry(posTable.getName() + ".png"));
						 zip.write(bytes);
						 IOUtils.closeQuietly(read);
						 zip.flush();
						 zip.closeEntry();
					 } catch (IOException e) {
						 log.error("下载失败！", e);
					 }
				 } catch (MalformedURLException e) {
					 e.printStackTrace();
				 }
			 }
		 }
		 IOUtils.closeQuietly(zip);
		 byte[] bytes = outputStream.toByteArray();
//由响应response直接输入到前端
		 response.reset();
		 try {
			 response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("桌台二维码.zip", "UTF-8"));
		 } catch (UnsupportedEncodingException e) {
			 e.printStackTrace();
		 }
		 response.addHeader("Content-Length", "" + bytes.length);
		 response.setContentType("application/octet-stream; charset=UTF-8");
		 try {
			 IOUtils.write(bytes, response.getOutputStream());
		 } catch (IOException e) {
			 e.printStackTrace();
		 }
	 }
}
